package com.korubo.web.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.korubo.kit.R;
import com.korubo.web.mapper.RoleMenuMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.korubo.web.model.Role;
import com.korubo.web.mapper.RoleMapper;
import com.korubo.web.service.RoleService;

import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Override
    public R list(Role role) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        Page<Role> page = new Page<>();
        if (null != role) {
            if (StrUtil.isNotBlank(role.getRoleName())) {
                queryWrapper.like("role_name", role.getRoleName());
            }
            if (StrUtil.isNotBlank(role.getRemaker())) {
                queryWrapper.like("remaker", role.getRemaker());
            }
            page = new Page<>(role.getPageNum(), role.getPageSize());
        }
        Page<Role> rolePage = roleMapper.selectPage(page, queryWrapper);
        return R.ok(rolePage);
    }

    @Override
    public R saveRoleMenu(Role role) {
        //先删除
        roleMenuMapper.removeByRoleId(role);
        roleMenuMapper.saveBatch(role);
        return R.ok();
    }

    @Override
    public R getRoleMenu(Role role) {
        List<String> byRoleId = roleMenuMapper.findByRoleId(role);
        return R.ok(byRoleId);
    }
}
